#include "KSADPrimaryGeneratorAction.hh"
#include "KSADPrimaryGeneratorMessenger.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleDefinition.hh"
#include "G4ThreeVector.hh"
#include "G4ios.hh"
#include "KSADGlobals.hh"

KSADPrimaryGeneratorAction::KSADPrimaryGeneratorAction()
{
	KSADPrimaryGeneratorMessenger *messenger = new KSADPrimaryGeneratorMessenger(this);	// Call KSADPrimaryGeneratorMessenger and pass this class to it
	(void)messenger;									// in order for it to call functions of this class

	rayGun = new G4ParticleGun(1);								// Set Defaults
	polarAngle = 0*deg;
	azimuthalAngle = 0*deg;
	startCrystalI = 1.2*m/crystalSize*3/4; startCrystalJ = 1.2*m/crystalSize*1/2;
	PFCI = 0; PFCJ = 0; PFCK = 0;

	G4ParticleTable *particleTable = G4ParticleTable::GetParticleTable();			// Find particle in G4ParticleTable,
	G4ParticleDefinition *particle = particleTable->FindParticle("gamma");			// and apply it to the rayGun
												//
	rayGun->SetParticleDefinition(particle);						//
	rayGun->SetParticleTime(0);								//
	rayGun->SetParticleEnergy(200*MeV);							//
	rayGunPositioning();									//

	path = ".";										// Set default ROOT data save path
}

void KSADPrimaryGeneratorAction::rayGunPositioning()	// Call any time KSAD defined rayGun attributes change
{
	int posI = crystalSize*(startCrystalI-1.2*m/crystalSize/2+.5)+PFCI;
	int posJ = crystalSize*(startCrystalJ-1.2*m/crystalSize/2+.5)+PFCJ;
	int posK = PFCK;

	rayGun->SetParticlePosition(G4ThreeVector(posI,posJ,posK));
	rayGun->SetParticleMomentumDirection(G4ThreeVector(cos(azimuthalAngle)*sin(polarAngle),sin(azimuthalAngle)*sin(polarAngle),cos(polarAngle)));
}

KSADPrimaryGeneratorAction::~KSADPrimaryGeneratorAction()
{
	delete rayGun;
}

void KSADPrimaryGeneratorAction::GeneratePrimaries(G4Event *event)	// Take G4Event given by G4RunManager and give to rayGun
{
	rayGun->GeneratePrimaryVertex(event);
}

void KSADPrimaryGeneratorAction::setPolarAngle(G4double newAngle)
{
	polarAngle = newAngle;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setAzimuthalAngle(G4double newAngle)
{
	azimuthalAngle = newAngle;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setStartCrystalI(G4int IPos)
{
	startCrystalI = IPos;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setStartCrystalJ(G4int JPos)
{
	startCrystalJ = JPos;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setPFCK(G4double KOff)
{
	PFCK = KOff;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setPFCI(G4double IOff)
{
	PFCI = IOff*crystalSize;
	rayGunPositioning();
}

void KSADPrimaryGeneratorAction::setPFCJ(G4double JOff)
{
	PFCJ = JOff*crystalSize;
	rayGunPositioning();
}


void KSADPrimaryGeneratorAction::setFilePath(G4String filePath)
{
	path = filePath;
}

G4String KSADPrimaryGeneratorAction::GetFilePath()
{
	return path;
}

G4float KSADPrimaryGeneratorAction::GetPolarAngle()
{
	return polarAngle;
}

G4float KSADPrimaryGeneratorAction::GetAzimuthalAngle()
{
	return azimuthalAngle;
}

G4float KSADPrimaryGeneratorAction::GetPFCK()
{
	return PFCK;
}
